package com.usooft.metaTalent.core.system.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.system.entity.RolePermission;
import com.usooft.metaTalent.core.system.mapper.RolePermissionMapper;
import com.usooft.metaTalent.core.system.query.RolePermissionQuery;
import com.usooft.metaTalent.core.system.service.RolePermissionService;
import com.usooft.metaTalent.core.system.wrapper.RolePermissionWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseRolePermissionService implements RolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public RolePermission get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<RolePermission> find(@NonNull Long id) {
        return Optional.ofNullable(rolePermissionMapper.get(id));
    }

    @Override
    public List<RolePermissionWrapper> list(RolePermissionQuery query) {
        return rolePermissionMapper.list(query);
    }

    @Override
    public Page<RolePermissionWrapper> page(RolePermissionQuery query) {
        int rows = rolePermissionMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : rolePermissionMapper.list(query));
    }

    protected Optional<RolePermission> findOne(RolePermissionQuery.Filter filter,
            RolePermissionQuery.Sorts sorts) {
        List<RolePermissionWrapper> wrappers = list(RolePermissionQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<RolePermission> findOne(RolePermissionQuery.Filter filter) {
        return findOne(filter, RolePermissionQuery.Sorts.builder().build());
    }

    @Override
    public int count(RolePermissionQuery.Filter filter) {
        return rolePermissionMapper.count(filter);
    }

    protected RolePermission internalAdd(Long roleId, String permission) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermission(permission);

        rolePermission.setCreatedDate(LocalDateTime.now());
        rolePermission.setVersion(0);
        rolePermissionMapper.add(rolePermission);
        return rolePermission;
    }

    protected void internalUpdate(@NonNull RolePermission rolePermission) {
        rolePermission.setLastModifiedDate(LocalDateTime.now());

        int rows = rolePermissionMapper.update(rolePermission);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能角色权限 (" + rolePermission.getId() + ") 的版本 (" + rolePermission.getVersion() + ") 已不是最新");
        }
        rolePermission.setVersion(rolePermission.getVersion() + 1);
    }

    protected void internalDelete(@NonNull RolePermission rolePermission) {
        int rows = rolePermissionMapper.delete(rolePermission);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("删除失败。可能角色权限 (" + rolePermission.getId() + ") 的版本 (" + rolePermission.getVersion() + ") 已不是最新");
        }
    }
}
